package com.ulic.customerManApplication.pager;

import com.ulic.customerManApplication.constant.CONSTANT;

public class PagerTag {
	public static String getStart(String title, PagerModel pagerModel) throws Exception {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<script>").append("var totalPage = ").append(pagerModel.getTotalPage()).append(";").append("</script>");
        buffer.append("<input type=\"hidden\" name=\"curPage\" value=\"").append(pagerModel.getCurPage())
                .append("\" id=\"curPage\"/>");
        buffer.append("<table width=\"99%\"   cellspacing=\"0\" cellpadding=\"4\" class=\"pagination\">");
        buffer.append("<tr>");
        buffer.append("<td nowrap=\"nowrap\"><strong>");
        buffer.append(title);
        buffer.append("</strong></td>");
        buffer.append("<td nowrap>(共");
        buffer.append(pagerModel.getTotalPage());
        buffer.append("页,");
        buffer.append(pagerModel.getTotal());
        buffer.append("条记录)");
        buffer.append("</td>");
        buffer.append("<td width=\"99%\"></td>");

        addStartHTMLCode(buffer, 1, pagerModel);
        
        return buffer.toString();
	}
	
	public static String getEnd(PagerModel pagerModel) throws Exception {
		StringBuffer buffer = new StringBuffer();

        buffer.append("<table width=\"99%\"  cellspacing=\"0\" cellpadding=\"4\" class=\"pagination\">");
        buffer.append("<tr>");
        buffer.append("<td nowrap>每页" + CONSTANT.PageSize + "条记录</td>").append("<td width=\"99%\">&nbsp;</td>");

        addHTMLCode(buffer, 2, pagerModel);
        addScriptFunction(buffer);
        
        return buffer.toString();
	}
	public static String getEnd2(PagerModel pagerModel) throws Exception {
		StringBuffer buffer = new StringBuffer();

        buffer.append("<table width=\"99%\"  cellspacing=\"0\" cellpadding=\"4\" class=\"pagination\">");
        buffer.append("<tr>");
        buffer.append("<td nowrap>每页" + 10 + "条记录</td>").append("<td width=\"99%\">&nbsp;</td>");

        addHTMLCode2(buffer, 2, pagerModel);
        addScriptFunction(buffer);
        
        return buffer.toString();
	}
	
	private static void addStartHTMLCode(StringBuffer buffer, int position, PagerModel pagerModel) {
        buffer.append("<td align=\"right\" nowrap>");
        buffer.append("</td>").append("</tr>").append("</table>");
    }
	
	private static void addHTMLCode(StringBuffer buffer, int position, PagerModel pagerModel) {
        buffer.append("<td align=\"right\" nowrap>");
        if (pagerModel.getCurPage() == 1) {
            buffer.append("<img src='html/images/page-first-disabled.gif'/>");
        } else {
            buffer.append("<a href=\"javascript:");
            buffer.append("firstPageClicked();\">").append("<img style='border-style:none;' src='html/images/page-first.gif'/>").append("</a>");
        }
        buffer.append("&nbsp;");

        if (pagerModel.getCurPage() == 1) {
            buffer.append("<img src='html/images/page-prev-disabled.gif'/>");
        } else {
            buffer.append("<a href=\"javascript:");
            buffer.append("prePageClicked();\">").append("<img style='border-style:none;' src='html/images/page-prev.gif'/>").append("</a>");
        }
        buffer.append("&nbsp;|&nbsp;");
        if (pagerModel.getCurPage() < pagerModel.getTotalPage()) {
            buffer.append("<a href=\"javascript:");
            buffer.append("nextPageClicked();\">").append("<img style='border-style:none;' src='html/images/page-next.gif'/>").append("</a>");
        } else {
            buffer.append("<img src='html/images/page-next-disabled.gif'/>");
        }
        buffer.append("&nbsp;");
        if (pagerModel.getCurPage() * CONSTANT.PageSize < pagerModel.getTotal()) {
            buffer.append("<a href=\"javascript:");
            buffer.append("lastPageClicked();\">").append("<img style='border-style:none;' src='html/images/page-last.gif'/>").append("</a>");
        } else {
            buffer.append("<img src='html/images/page-last-disabled.gif'/>");
        }
        buffer.append("&nbsp;&nbsp;").append("第");
        if (position == 1)
            buffer.append("<input type=\"text\" value=\"")
                    .append(pagerModel.getCurPage())
                    .append("\" size=\"2\" id=\"topCurPage\" onkeyup=\"this.value=this.value.replace(/[\\D]/g,'');\" onkeydown=\"if(event.keyCode == '13') return topCurPageChanged()\">");
        else
            buffer.append("<input type=\"text\" value=\"")
                    .append(pagerModel.getCurPage())
                    .append("\" size=\"2\" id=\"bottomCurPage\" onkeyup=\"this.value=this.value.replace(/[\\D]/g,'');\" onkeydown=\"if(event.keyCode == '13') return bottomCurPageChanged()\">");
        buffer.append("页");

        buffer.append("<input type=\"button\" value=\"go\" onclick=\"");
        if (position == 1) {
            buffer.append("return topCurPageChanged()\">");
        } else {
            buffer.append("return bottomCurPageChanged()\">");
        }
        buffer.append("</td>").append("</tr>").append("</table>");
    }
	
	private static void addHTMLCode2(StringBuffer buffer, int position, PagerModel pagerModel) {
        buffer.append("<td align=\"right\" nowrap>");
        if (pagerModel.getCurPage() == 1) {
            buffer.append("<img src='html/images/page-first-disabled.gif'/>");
        } else {
            buffer.append("<a href=\"javascript:");
            buffer.append("firstPageClicked();\">").append("<img style='border-style:none;' src='html/images/page-first.gif'/>").append("</a>");
        }
        buffer.append("&nbsp;");

        if (pagerModel.getCurPage() == 1) {
            buffer.append("<img src='html/images/page-prev-disabled.gif'/>");
        } else {
            buffer.append("<a href=\"javascript:");
            buffer.append("prePageClicked();\">").append("<img style='border-style:none;' src='html/images/page-prev.gif'/>").append("</a>");
        }
        buffer.append("&nbsp;|&nbsp;");
        if (pagerModel.getCurPage() < pagerModel.getTotalPage()) {
            buffer.append("<a href=\"javascript:");
            buffer.append("nextPageClicked();\">").append("<img style='border-style:none;' src='html/images/page-next.gif'/>").append("</a>");
        } else {
            buffer.append("<img src='html/images/page-next-disabled.gif'/>");
        }
        buffer.append("&nbsp;");
        if (pagerModel.getCurPage() * 10< pagerModel.getTotal()) {
            buffer.append("<a href=\"javascript:");
            buffer.append("lastPageClicked();\">").append("<img style='border-style:none;' src='html/images/page-last.gif'/>").append("</a>");
        } else {
            buffer.append("<img src='html/images/page-last-disabled.gif'/>");
        }
        buffer.append("&nbsp;&nbsp;").append("第");
        if (position == 1)
            buffer.append("<input type=\"text\" value=\"")
                    .append(pagerModel.getCurPage())
                    .append("\" size=\"2\" id=\"topCurPage\" onkeyup=\"this.value=this.value.replace(/[\\D]/g,'');\" onkeydown=\"if(event.keyCode == '13') return topCurPageChanged()\">");
        else
            buffer.append("<input type=\"text\" value=\"")
                    .append(pagerModel.getCurPage())
                    .append("\" size=\"2\" id=\"bottomCurPage\" onkeyup=\"this.value=this.value.replace(/[\\D]/g,'');\" onkeydown=\"if(event.keyCode == '13') return bottomCurPageChanged()\">");
        buffer.append("页");

        buffer.append("<input type=\"button\" value=\"go\" onclick=\"");
        if (position == 1) {
            buffer.append("return topCurPageChanged()\">");
        } else {
            buffer.append("return bottomCurPageChanged()\">");
        }
        buffer.append("</td>").append("</tr>").append("</table>");
    }
	
	
	 private static void addScriptFunction(StringBuffer buffer) {
	    	buffer.append("\n<script type=\"text/javascript\">\n");
	        buffer.append("function topCurPageChanged(){\n")
	        	.append("document.getElementById(\"curPage\").value = ")
	        		.append("document.getElementById(\"topCurPage\").value;\n")
	            .append("document.listForm.submit();\n\t}\n");
	        buffer.append("function bottomCurPageChanged(){\n")
	    		.append("document.getElementById(\"curPage\").value = ")
	    			.append("document.getElementById(\"bottomCurPage\").value;\n")
	            .append("document.listForm.submit();\n\t}\n");
	        buffer.append("function firstPageClicked(){\n").
	        	append("document.getElementById(\"curPage\").value = \"1\";\n")
	            .append("document.listForm.submit();\n\t}\n");
	        buffer.append("function prePageClicked(){\n")
	        	.append("document.getElementById(\"curPage\").value = ")
	        	.append("document.getElementById(\"curPage\").value - 1;\n")
	            .append("document.listForm.submit();\n\t}\n");
	        buffer.append("function nextPageClicked(){\n")
	        	.append("document.getElementById(\"curPage\").value = ")
	        		.append("Math.round(document.getElementById(\"curPage\").value) + 1;\n")
	            .append("document.listForm.submit();\n\t}\n");
	        buffer.append("function lastPageClicked(){\n")
	        	.append("document.getElementById(\"curPage\").value = totalPage;\n")
	        	.append("document.listForm.submit();\n\t}\n");
	        
	        buffer.append("</script>");
	    }
}
